package com.sunday.aomicaichang.ui.product;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;

import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.MD5Utils;
import com.sunday.aomicaichang.R;
import com.sunday.aomicaichang.adpter.ProductClassifyChildAdapter;
import com.sunday.aomicaichang.adpter.ProductClassifyParentAdapter;
import com.sunday.member.entity.product.ProductCategory;
import com.sunday.member.http.MemberClient;
import com.sunday.member.utils.SignUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class ProductClassifyActivity extends BaseActivity {


    @Bind(R.id.btn_back)
    ImageView btnBack;
    @Bind(R.id.edit_query)
    EditText editQuery;
    @Bind(R.id.product_category)
    ListView productCategory;
    @Bind(R.id.product_category_list)
    GridView productCategoryList;

    private List<ProductCategory> parentList = new ArrayList<>();
    private List<ProductCategory> childList = new ArrayList<>();
    private ProductClassifyParentAdapter parentAdapter;
    private ProductClassifyChildAdapter childAdapter;
    private int parentId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_classify);
        ButterKnife.bind(this);
        parentId = getIntent().getIntExtra("parentId", 0);
        initAdapters();
        getParentCategory();

    }


    @OnClick({R.id.btn_back, R.id.edit_query})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_back:
                onBackPressed();
                break;
            case R.id.edit_query:
                intent = new Intent(mContext, SearchProductListActivity.class);
                startActivity(intent);
                break;
        }
    }


    private void initAdapters() {
        parentAdapter = new ProductClassifyParentAdapter(mContext, parentList);
        productCategory.setAdapter(parentAdapter);
        childAdapter = new ProductClassifyChildAdapter(mContext, childList);
        productCategoryList.setAdapter(childAdapter);
        productCategory.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                parentAdapter.setSelectPosition(position);
                parentId = parentList.get(position).getId();
                if (parentList.get(position).getChildren() != null) {
                    childList.clear();
                    childList.addAll(parentList.get(position).getChildren());
                }
                parentAdapter.notifyDataSetChanged();
                childAdapter.notifyDataSetChanged();

            }
        });
        productCategoryList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ProductCategory productBrand = (ProductCategory) parent.getAdapter().getItem(position);
                intent = new Intent(mContext, SearchProductListActivity.class);
                intent.putExtra("catId", productBrand.getId());
                startActivity(intent);
            }
        });
    }


    private void getParentCategory() {
        String nonce = MD5Utils.random();
        String[] str1 = new String[]{"categoryId", "nonce"};
        String[] str2 = new String[]{String.valueOf(parentId), nonce};
        Call<ResultDO<List<ProductCategory>>> call = MemberClient.getApiAdapter().getProductTree(parentId, nonce, SignUtil.getSign(str1, str2));
        call.enqueue(new Callback<ResultDO<List<ProductCategory>>>() {
            @Override
            public void onResponse(Call<ResultDO<List<ProductCategory>>> call, Response<ResultDO<List<ProductCategory>>> response) {
                if (isFinish) {
                    return;
                }
                if (response.body() == null) {
                    return;
                }
                if (response.body().getCode() == 0) {
                    parentList.clear();
                    parentList.addAll(response.body().getResult());
                    parentAdapter.notifyDataSetChanged();
                    if (parentList != null && parentList.size() > 0)
                        if (parentId != 0) {
                            for (int i = 0; i < parentList.size(); i++) {
                                if (parentId == parentList.get(i).getId()) {
                                    if (parentList.get(i).getChildren() != null) {
                                        childList.clear();
                                        childList.addAll(parentList.get(i).getChildren());
                                        parentAdapter.setSelectPosition(i);
                                        break;
                                    }
                                }
                            }
                        } else {
                            parentId = parentList.get(0).getId();
                            if (parentList.get(0).getChildren() != null) {
                                childList.clear();
                                childList.addAll(parentList.get(0).getChildren());
                            }
                        }
                    parentAdapter.notifyDataSetChanged();
                    childAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(Call<ResultDO<List<ProductCategory>>> call, Throwable t) {

            }
        });
    }

}
